package cn.tedu.bece.admin.system.controller;

import cn.tedu.bece.admin.common.annotation.Log;
import cn.tedu.bece.admin.common.response.JsonResult;
import cn.tedu.bece.admin.common.response.PageObject;
import cn.tedu.bece.admin.system.pojo.dto.SysDictAddParam;
import cn.tedu.bece.admin.system.pojo.dto.SysDictQuery;
import cn.tedu.bece.admin.system.pojo.dto.SysDictUpdateParam;
import cn.tedu.bece.admin.system.pojo.entity.SysDict;
import cn.tedu.bece.admin.system.service.ISysDictService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "60.字典管理")
@RestController
@RequestMapping("/api/sys/dicts")
public class SysDictController {

    @Autowired
    private ISysDictService sysDictService;

    @Log(value = "新增字典")
    @ApiOperation(value = "新增字典")
    @ApiOperationSupport(order = 100)
    @PostMapping
    public JsonResult add(@Validated @RequestBody SysDictAddParam sysDictAddParam) {
        sysDictService.insert(sysDictAddParam);
        return JsonResult.success();
    }

    @Log(value = "删除字典")
    @ApiOperation(value = "删除字典")
    @ApiOperationSupport(order = 200)
    @DeleteMapping
    public JsonResult delete(@RequestParam("ids") List<Long> ids) {
        sysDictService.deleteByIds(ids);
        return JsonResult.success();
    }

    @Log(value = "修改字典")
    @ApiOperation(value = "修改字典")
    @ApiOperationSupport(order = 300)
    @PutMapping
    public JsonResult update(@Validated @RequestBody SysDictUpdateParam sysDictUpdateParam) {
        sysDictService.updateById(sysDictUpdateParam);
        return JsonResult.success();
    }

    @ApiOperation(value = "查询字典详情")
    @ApiOperationSupport(order = 400)
    @GetMapping("/{id}")
    public JsonResult getDetail(@PathVariable("id") Long id) {
        SysDict sysDict = sysDictService.selectById(id);
        return JsonResult.success(sysDict);
    }

    @ApiOperation(value = "查询字典列表")
    @ApiOperationSupport(order = 410)
    @GetMapping
    public JsonResult list(Integer pageNum, Integer pageSize, SysDictQuery query) {
        PageObject<SysDict> pageObject = PageObject.page(pageNum, pageSize, () -> sysDictService.selectList(query));
        return JsonResult.success(pageObject);
    }

}
